﻿// Author: 刘理想
// Created Date: 2014-12-02

using System.IO;
using System.Text;

namespace LabelPrint.Util
{
    public class FileHelper
    {
        /// <summary>
        ///     返回文件扩展名
        /// </summary>
        public static string GetExtension(string path)
        {
            return Path.GetExtension(path);
        }

        /// <summary>
        ///     返回文件名
        /// </summary>
        public static string GetFileName(string path)
        {
            return Path.GetFileName(path);
        }

        /// <summary>
        ///     返回不含扩展名的文件名
        /// </summary>
        public static string GetFileNameWithoutExtension(string path)
        {
            return Path.GetFileNameWithoutExtension(path);
        }

        /// <summary>
        ///     返回文件夹名
        /// </summary>
        public static string GetDirectory(string path)
        {
            return Path.GetDirectoryName(path);
        }

        /// <summary>
        ///     打开文件
        /// </summary>
        /// <param name="path">文件路径</param>
        /// <param name="encoding">默认是utf-8</param>
        /// <returns>返回StreamReader类型</returns>
        public static StreamReader Open(string path, string encoding = "utf-8")
        {
            return new StreamReader(path, Encoding.GetEncoding(encoding));
        }

        /// <summary>
        ///     返回指定路径下的文件
        /// </summary>
        /// <param name="path">路径</param>
        /// <param name="filterExtension">文件扩展名</param>
        /// <param name="searchOption">查找选项</param>
        /// <returns>返回文件列表</returns>
        public static string[] FileList(string path, string filterExtension = "*.DAT",
            SearchOption searchOption = SearchOption.AllDirectories)
        {
            return Directory.GetFiles(path, filterExtension, searchOption);
        }
    }
}